using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace zevenseas.Community.Web.UI.WebControls.WebParts
{
    public class ChoiceFilterEditor : EditorPart
    {
        private DropDownList listDropDown;
        private DropDownList listColumnDropDown;
        private DropDownList displayType;

        private LiteralControl choiceLabel;
        private Panel toolPanePanel;

        protected override void CreateChildControls()
        {
            Title = "Choice Filter Configuration";
            toolPanePanel = new Panel();
            toolPanePanel.Attributes.CssStyle.Add("padding-left", "5px");
            toolPanePanel.Attributes.CssStyle.Add("padding-top", "5px");
            Controls.Add(toolPanePanel);

            toolPanePanel.Controls.Add(new LiteralControl("List:</br>"));
            listDropDown = new DropDownList();
            toolPanePanel.Controls.Add(listDropDown);

            SPListCollection webLists = SPContext.Current.Web.Lists;
            webLists.ListsForCurrentUser = true;

            listDropDown.AutoPostBack = true;
            listDropDown.EnableViewState = true;
            listDropDown.DataSource = webLists;
            listDropDown.DataTextField = "Title";
            listDropDown.DataValueField = "ID";
            listDropDown.DataBind();
            listDropDown.SelectedIndexChanged += listDropDown_SelectedIndexChanged;

            choiceLabel = new LiteralControl("</br>Choice Column:</br>"); 
            toolPanePanel.Controls.Add(choiceLabel);

            listColumnDropDown = new DropDownList();
            listColumnDropDown.AutoPostBack = true;
            toolPanePanel.Controls.Add(listColumnDropDown);

            if (ViewState["ListID"] == null)
            {
                ViewState["ListID"] = listDropDown.SelectedValue;
                CreateColumnDropDown();
            }

            toolPanePanel.Controls.Add(new LiteralControl("</br>Display Type:</br>"));
            displayType = new DropDownList();
            displayType.DataSource = Enum.GetNames(typeof(ChoiceFilterDisplayType));
            displayType.DataBind();
            toolPanePanel.Controls.Add(displayType);

            TagLine productTagLine = new TagLine();
            productTagLine.ProductName = "Choice Filter";
            productTagLine.Version = "1.1";
            productTagLine.ProductId = "87";
            toolPanePanel.Controls.Add(productTagLine);

            base.CreateChildControls();
        }

        private void listDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["ListID"] = listDropDown.SelectedValue;
            CreateColumnDropDown();
        }

        private void CreateColumnDropDown()
        {
            if (!string.IsNullOrEmpty(ViewState["ListID"].ToString()))
            {
                SPList choiceColumnList = SPContext.Current.Web.Lists[new Guid(ViewState["ListID"].ToString())];

                List<SPField> choiceColumns = new List<SPField>();
                foreach (SPField columnField in choiceColumnList.Fields)
                {
                    if (columnField.Type == SPFieldType.Choice || columnField.Type == SPFieldType.MultiChoice)
                    {
                        choiceColumns.Add(columnField);
                    }
                }

                if (choiceColumns.Count > 0)
                {
                    listColumnDropDown.DataSource = choiceColumns;
                    listColumnDropDown.DataTextField = "Title";
                    listColumnDropDown.DataValueField = "ID";
                    listColumnDropDown.DataBind();
                    listColumnDropDown.Visible = true;
                    choiceLabel.Text = "</br>Choice Column:</br>";
                    choiceLabel.Visible = true;
                }
                else
                {
                    listColumnDropDown.Visible = false;
                    choiceLabel.Text = "</br>No Choice Fields</br>";
                }
            }
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();

            ChoiceFilter webPart = WebPartToEdit as ChoiceFilter; 
            
            if (webPart != null)
            {
                webPart.ListId = listDropDown.SelectedValue;
                webPart.ChoiceFieldId = listColumnDropDown.SelectedValue;
                webPart.DisplayType = (ChoiceFilterDisplayType)Enum.Parse(typeof(ChoiceFilterDisplayType), displayType.SelectedValue);
            }
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            ChoiceFilter webPart = WebPartToEdit as ChoiceFilter;

            if (webPart != null && !string.IsNullOrEmpty(webPart.ListId))
            {
                //ViewState["ListID"] = webPart.ListId;
                if (listDropDown.Items.FindByValue(webPart.ListId) != null)
                {
                    listDropDown.ClearSelection();
                    listDropDown.Items.FindByValue(webPart.ListId).Selected = true;   
                }
                ViewState["ListID"] = listDropDown.SelectedValue;
                CreateColumnDropDown();
                if (listColumnDropDown.Items.FindByValue(webPart.ChoiceFieldId) != null)
                {
                    listColumnDropDown.ClearSelection();
                    listColumnDropDown.Items.FindByValue(webPart.ChoiceFieldId).Selected = true;
                }
                if (displayType.Items.FindByValue(webPart.DisplayType.ToString()) != null)
                {
                    displayType.ClearSelection();
                    displayType.Items.FindByValue(webPart.DisplayType.ToString()).Selected = true;
                }

            }
        }
    }
}